package org.dtrd.modules.device.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.common.util.CheckUtil;
import org.dtrd.config.system.SystemState;
import org.dtrd.modules.device.config.DeviceConstant;
import org.dtrd.modules.device.entity.po.DtrdEntRdBioland;
import org.dtrd.modules.device.entity.request.BindDeviceRequest;
import org.dtrd.modules.device.mapper.DtrdEntRdBiolandMapper;
import org.dtrd.modules.device.service.IDtrdEntRdBiolandService;
import org.dtrd.common.api.vo.Result;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 萧
 * @date 2021-09-01 00:02:01
 **/
@Service
@DS("multi-datasource1")
public class DtrdEntRdBiolandServiceImpl extends ServiceImpl<DtrdEntRdBiolandMapper, DtrdEntRdBioland> implements IDtrdEntRdBiolandService {

    /**
     * 设备操作 解绑、绑定
     *
     * @param request
     * @return
     */
    @Override
    public Result<?> bindDevice(BindDeviceRequest request) {
        if (DeviceConstant.BindState.isBind(request.getOperation())) {
            return doBind(request);
        } else {
            return doUnbind(request);
        }
    }

    /**
     * 执行绑定逻辑
     *
     * @param request
     * @return
     */
    private Result<?> doBind(BindDeviceRequest request) {
        // 查询用户绑定记录
        DtrdEntRdBioland recordByPatientId = baseMapper.selectRecordByCondition(request.getPatientId(), null, DeviceConstant.BindState.BIND.getState(), request.getDeviceCategory());
        // 查询设备关联记录
        DtrdEntRdBioland recordBySn = baseMapper.selectRecordByCondition(null, request.getDeviceSn(), DeviceConstant.BindState.BIND.getState(), request.getDeviceCategory());
        if (recordByPatientId == null && recordBySn == null) {
            // 用户未曾绑定设备 设备未曾绑定用户
            // 完成绑定
            saveRecord(request);
            return Result.OK(SystemState.ResponseState.STATE_SUCCESS.getMsg());
        } else if (recordByPatientId != null && recordBySn != null) {
            // 用户已绑定设备 设备已绑定用户
            if (recordByPatientId.getDataId().equals(recordBySn.getDataId())) {
                // 判断用户与设备关联一致
                //不作处理返回成功
                return Result.OK(SystemState.ResponseState.STATE_SUCCESS.getMsg());
            } else {
                return Result.error(SystemState.ResponseState.STATE_DATA_ERROR.getCode(), SystemState.ResponseState.STATE_DATA_ERROR.getMsg());
            }
        } else {
            // 设备已绑定 或者 用户已绑定
            // 返回错误信息
            return Result.error(SystemState.ResponseState.STATE_OPERATION_FAILURE.getCode(), SystemState.ResponseState.STATE_OPERATION_FAILURE.getMsg());
        }
    }

    /**
     * 设备解绑
     *
     * @param request
     * @return
     */
    private Result<?> doUnbind(BindDeviceRequest request) {
        DtrdEntRdBioland record = baseMapper.selectRecordByCondition(request.getPatientId(), request.getDeviceSn(), DeviceConstant.BindState.BIND.getState(), request.getDeviceCategory());
        if (record != null) {
            // 查询当前关联状态 执行解绑
            record.setDeviceBindState(DeviceConstant.BindState.UNBIND.getState());
            baseMapper.updateById(record);
        }
        return Result.OK(SystemState.ResponseState.STATE_SUCCESS.getMsg());
    }

    /**
     * 插入绑定记录
     *
     * @param request
     */
    private void saveRecord(BindDeviceRequest request) {
        Integer patientId = request.getPatientId();
        String deviceSn = request.getDeviceSn();
        DtrdEntRdBioland entity = new DtrdEntRdBioland();
        entity.setCreateTime(new Date());
        entity.setUserId(patientId);
        entity.setDeviceSn(deviceSn);
        entity.setDeviceImei(CheckUtil.isEmpty(request.getDeviceImei()) ? deviceSn : request.getDeviceImei());
        entity.setDeviceImsi(request.getDeviceImsi());
        entity.setDeviceBindState(DeviceConstant.BindState.BIND.getState());
        entity.setDeviceCategory(request.getDeviceCategory());
        baseMapper.insert(entity);
    }

    /**
     * 根据设备序列号和设备类型查询绑定记录
     *
     * @param serialNumber 序列号
     * @param deviceType   设备类型
     * @return
     */
    @Override
    public DtrdEntRdBioland selectOntByDeviceSnAndDeviceType(String serialNumber, String deviceType) {
        return baseMapper.selectRecordByCondition(null, serialNumber, DeviceConstant.BindState.BIND.getState(), Integer.valueOf(deviceType));
    }

    @Override
    public DtrdEntRdBioland getBindRecord(Integer patientId, Integer deviceCategory) {
        return baseMapper.selectRecordByCondition(patientId, null, DeviceConstant.BindState.BIND.getState(), deviceCategory);
    }

}
